package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.annotation.OperationLog;
import com.mall.db.domain.Commodity;
import com.mall.db.domain.Coupon;
import com.mall.db.service.CommodityService;
import com.mall.db.service.CouponService;
import com.mall.utils.StringUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/system/cc")
public class couponController extends BaseController<Coupon>{

    @Resource(name = "couponService")
    private CouponService cs;
    @Resource(name = "commodityService")
    private CommodityService coms;

    @Override
    @OperationLog(value = "查询优惠券列表",type = 0)
    public Map<String, Object> find(Coupon coupon) {
        PageHelper.startPage(coupon.getPage(),coupon.getLimit());
        QueryWrapper qw = new QueryWrapper();
        List<Coupon> list = cs.list(qw);
        PageInfo<Coupon> page = new PageInfo<>(list,coupon.getLimit());
        return findMap(page.getList(),page.getTotal());
    }

    @Override
    @OperationLog(value = "添加新的优惠券",type = 0)
    @ResponseBody
    public Map<String, Object> add(Coupon coupon) {
        if (coupon.getUseRange().equals("指定商品")&&coupon.getCommodityList()!=null){
            Object ids[] = coupon.getCommodityList().toArray();
            String CommodityListIds = ArrayUtils.toString(ids,",");
            coupon.setCommodityListIds(CommodityListIds);
        }
        if (coupon.getTimeType()==0){
            if (coupon.getDayRange()==null){
                return this.iud("相对领券天数不能为空,添加",false,coupon);
            }
        }else if (coupon.getTimeType()==1){
            if (coupon.getStartTime()==null){
                return this.iud("优惠券有效开始时间不能为空,添加",false,coupon);
            }else if (coupon.getEndTime()==null){
                return this.iud("优惠券有效结束时间不能为空,添加",false,coupon);
            }
        }
        Random r = new Random();
        if (!coupon.getType().isEmpty()&&coupon.getType().equals("2")){//优惠券类型为兑换码兑换，自动生成兑换码
            String str = "";
            for (int i = 0;i < 7;i++){//自动生成一个八位数的兑换码
                int x = r.nextInt(25)+1;
                str = str +(char)(x + 'a');
            }
            coupon.setExchangeCode(str.toUpperCase());
        }
        boolean b = cs.save(coupon);
        return this.iud("新增优惠券",b,coupon);
    }

    @Override
    @OperationLog(value = "修改优惠券内容",type = 0)
    public Map<String, Object> update(Coupon coupon) {
        if (coupon.getTimeType()==0){
            if (coupon.getDayRange()==null){
                return this.iud("相对领券天数不能为空,添加",false,coupon);
            }
        }else if (coupon.getTimeType()==1){
            if (coupon.getStartTime()==null){
                return this.iud("优惠券有效开始时间不能为空,添加",false,coupon);
            }else if (coupon.getEndTime()==null){
                return this.iud("优惠券有效结束时间不能为空,添加",false,coupon);
            }
        }
        QueryWrapper qw = new QueryWrapper();
        if(coupon.getTimeType()==0){//当时间范围是领券相对天数时，清除开始时间与结束时间
            coupon.setStartTime(null);
            coupon.setEndTime(null);
        } else if (coupon.getTimeType()==1) {//当时间范围是指定绝对时间，清除相对天数
            coupon.setDayRange(null);
        }
        if (!coupon.getType().equals("2")){//当优惠券类型不为兑换码时，清空兑换码
            coupon.setExchangeCode(null);
        }else if (coupon.getType().equals("2") && coupon.getExchangeCode().isEmpty()){
            Random r = new Random();
            String str = "";
            for (int i = 0;i < 7;i++){//自动生成一个八位数的兑换码
                int x = r.nextInt(25)+1;
                str = str +(char)(x + 'a');
            }
            coupon.setExchangeCode(str.toUpperCase());
        }
        qw.eq("id",coupon.getId());
        boolean b = cs.update(coupon,qw);
        return this.iud("更新优惠券",b,coupon);
    }

    @Override
    @OperationLog(value = "删除优惠券信息",type = 0)
    public Map<String, Object> delete(Coupon coupon) {
        boolean b = cs.removeById(coupon.getId());
        return this.iud("删除优惠券",b,coupon);
    }

    @RequestMapping("/findCommodityList")
    public List<Commodity> findCommodityList(){
        QueryWrapper qw = new QueryWrapper();
        List<Commodity> list = coms.list(qw);
        return list;
    }

    @RequestMapping("/getCommodity")
    @ResponseBody
    public List<Commodity> getCommodity(@RequestParam(value = "ids") String Ids){
        /*Ids.replace("{","");
        Ids.replace("}","");*/
        Ids = StringUtil.deleteAppointString(Ids,'{');
        Ids = StringUtil.deleteAppointString(Ids,'}');
        String[] ids = Ids.split(",");
        List<Commodity> list = new ArrayList<>();
        for (int i = 0;i<ids.length;i++){
            Commodity c = new Commodity();
            c.setId(Integer.parseInt(ids[i]));
            c =  coms.getById(c);
            list.add(c);
        }
        return list;
    }
}
